﻿using Admin2024.Application.Contracts.Permission;
using Admin2024.Application.Contracts.Permission.Dto;
using Admin2024.Application.Contracts.User.Dto;
using Admin2024.Application.Permission.Dto;
using Admin2024.Domain.DomainService.System.UserService;
using Admin2024.Domain.Entity.System;
using Admin2024.Domain.IDomainService.System.IPermissionService;
using Admin2024.Domain.ObjValue;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2024.Application.Permission
{
    public class PermissionService : IPermissionService
    {

        private readonly IPermissionManagementService _permission;

        private readonly IOperationManagementService _operation;

        private readonly IResourceManagementService _resource;
        private readonly IMapper mapper;

        public PermissionService(IPermissionManagementService permission,IMapper mapper, IOperationManagementService operation, IResourceManagementService resource)
        {
            this._permission = permission;
            this.mapper = mapper;
            _operation = operation;
            _resource = resource;
        }

        /// <summary>
        /// 创建操作权限
        /// </summary>
        /// <param name="appResource"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<DomainResult<OperationCreateSucessDto>> CreateOperation(OperationCreateDto operationCreateDto )
        {
            var appOperation = mapper.Map<AppOperation>(operationCreateDto);
            var operation = await _operation.CreateOperation(appOperation);
            if (!operation.IsSuccess)
            {
                return DomainResult<OperationCreateSucessDto>.Error(operation.Message);
            }

            var operationCreateSucessDto = mapper.Map<OperationCreateSucessDto>(operation.Data);
            return DomainResult<OperationCreateSucessDto>.Success(operationCreateSucessDto);
        }
        /// <summary>
        /// 删除操作权限
        /// </summary>
        /// <param name="operId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<DomainResult<AppOperation>> DeleteOperation(Guid operId)
        {
            var operation = await _operation.DeleteOperation(operId);

            if (!operation.IsSuccess)
            {
                return DomainResult<AppOperation>.Error(operation.Message);
            }
            return DomainResult<AppOperation>.Success(operation.Data);
        }

        /// <summary>
        /// 创建权限
        /// </summary>
        /// <param name="permissionCreateDto"></param>
        /// <returns></returns>
        public async Task<DomainResult<PermissionCreateSucessDto>> CreatePermission(PermissionCreateDto permissionCreateDto)
        {
            var appPer = mapper.Map<AppPermission>(permissionCreateDto);    
            var permisson = await _permission.CreatePermission(appPer);
            if (!permisson.IsSuccess)
            {
                return DomainResult<PermissionCreateSucessDto>.Error(permisson.Message);
            }

            var permissionCreateSucessDto = mapper.Map<PermissionCreateSucessDto>(permisson.Data);
            return DomainResult<PermissionCreateSucessDto>.Success(permissionCreateSucessDto);
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="PerId"></param>
        /// <returns></returns>
        public async Task<DomainResult<AppPermission>> DeletePermission(Guid PerId)
        {
            var permission = await _permission.DeletePermission(PerId);

            if (!permission.IsSuccess)
            {
                return DomainResult<AppPermission>.Error(permission.Message);
            }
            return DomainResult<AppPermission>.Success(permission.Data);
        }

        /// <summary>
        /// 创建资源权限
        /// </summary>
        /// <param name="appResource"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public async Task<DomainResult<ResourceCreateSucessDto>> CreateResource(ResourceCreateDto  resourceCreateDto)
        {
            var appResource = mapper.Map<AppResource>(resourceCreateDto);
            var Resource = await _resource.CreateResource(appResource);
            if (!Resource.IsSuccess)
            {
                return DomainResult<ResourceCreateSucessDto>.Error(Resource.Message);
            }

            var resourceCreateSucessDto = mapper.Map<ResourceCreateSucessDto>(Resource.Data);
            return DomainResult<ResourceCreateSucessDto>.Success(resourceCreateSucessDto);
        }

        /// <summary>
        /// 删除资源权限
        /// </summary>
        /// <param name="resId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<DomainResult<AppResource>> DeleteResource(Guid resId)
        {
            var permission = await _resource.DeleteResource(resId);

            if (!permission.IsSuccess)
            {
                return DomainResult<AppResource>.Error(permission.Message);
            }
            return DomainResult<AppResource>.Success(permission.Data);
        }

        public async Task<DomainResult<List<OperationDto>>> GetOperationWithPagination(int page, int pageSize)
        {
            var Operation = await _operation.GetOperationWithPagination(page, pageSize);
            if (!Operation.IsSuccess)
            {
                return DomainResult<List<OperationDto>>.Error(Operation.Message);
            }
            var operationDto = mapper.Map<List<OperationDto>>(Operation.Data); 
            return DomainResult<List<OperationDto>>.Success(operationDto);
        }

        public async Task<DomainResult<List<PermissionDto>>> GetPermissionWithPagination(int page, int pageSize)
        {
            var Permission = await _permission.GetPermissionWithPagination(page, pageSize);
            if (!Permission.IsSuccess)
            {
                return DomainResult<List<PermissionDto>>.Error(Permission.Message);
            }
            var operationDto = mapper.Map<List<PermissionDto>>(Permission.Data);
            return DomainResult<List<PermissionDto>>.Success(operationDto);
        }

        public async Task<DomainResult<List<ResourceDto>>> GetResourceWithPagination(int page, int pageSize)
        {
            var Resource = await _resource.GetResourceWithPagination(page, pageSize);
            if (!Resource.IsSuccess)
            {
                return DomainResult<List<ResourceDto>>.Error(Resource.Message);
            }
            var operationDto = mapper.Map<List<ResourceDto>>(Resource.Data);
            return DomainResult<List<ResourceDto>>.Success(operationDto);
        }

        public async Task<DomainResult<ResourceDto>> GetAppResourceById(Guid id)
        {
            var Resource = await _resource.GetAppResourceById(id);
            if (!Resource.IsSuccess)
            {
                return  DomainResult<ResourceDto>.Error(Resource.Message); 
            }
            
            var ResourceDto = mapper.Map<ResourceDto>(Resource.Data);
            return DomainResult<ResourceDto>.Success(ResourceDto);
        }

        public async Task<DomainResult<PermissionDto>> GetAppPermissionById(Guid id)
        {
            var Permission = await _permission.GetAppPermissionById(id);
            if (!Permission.IsSuccess)
            {
                return DomainResult<PermissionDto>.Error(Permission.Message);
            }

            var PermissionDto = mapper.Map<PermissionDto>(Permission.Data);
            return DomainResult<PermissionDto>.Success(PermissionDto);
        }

        public async Task<DomainResult<OperationDto>> GetOperationById(Guid id)
        {
            var operation = await _operation.GetOperationById(id);
            if (!operation.IsSuccess)
            {
                return DomainResult<OperationDto>.Error(operation.Message);
            }

            var operationDto = mapper.Map<OperationDto>(operation.Data);
            return DomainResult<OperationDto>.Success(operationDto);
        }

        public async Task<DomainResult<ResourceDto>> UpdateResourceProfile(Guid Id, ResourceUpdateDto entity)
        {
            var resourceData = await _resource.GetAppResourceById(Id);
            var resource = resourceData.Data;
            if (!resourceData.IsSuccess)
            {
                return DomainResult<ResourceDto>.Error(resourceData.Message);
            }


            var resUpdate = mapper.Map<AppResource>(entity);
            var up = await _resource.UpdateResourceProfile(Id, resUpdate);
            var appResourceDto = mapper.Map<ResourceDto>(up.Data);
            return DomainResult<ResourceDto>.Success(appResourceDto);
        }

        public async Task<DomainResult<OperationDto>> UpdateOperationProfile(Guid Id, OperationUpdateDto entity)
        {
            var operationData = await _operation.GetOperationById(Id);
            var resource = operationData.Data;
            if (!operationData.IsSuccess)
            {
                return DomainResult<OperationDto>.Error(operationData.Message);
            }


            var userUpdate = mapper.Map<AppOperation>(entity);
            var up = await _operation.UpdateOperationProfile(Id, userUpdate);
            var appOperationDto = mapper.Map<OperationDto>(up.Data);
            return DomainResult<OperationDto>.Success(appOperationDto);
        }
    }
}
